using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using DAL;
using BUS;
using Utility;

public partial class admin_Suahinhanh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_mahinh.Text = id.ToString();

            tbHinhSanPham ha = tbHinhSanPhamBUS.GetByID(id);
            //txt_duongdan.Text = ha.Duongdan ;
            img_HinhMinhHoa.ImageUrl = "~/Photos/Thumb/" + ha.Tenhinh ;
        }
        catch
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbHinhSanPham ha = tbHinhSanPhamBUS.GetByID(id);

            if (fu_HinhMinhHoa.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_HinhMinhHoa.FileName;
                fu_HinhMinhHoa.SaveAs(Server.MapPath("~/Photos/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
                // xoa hinh cull
                if (ha.Tenhinh  != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + ha.Tenhinh ));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + ha.Tenhinh ));
                }
                ha.Tenhinh  = filename;
               // ha.Duongdan  = "~/Photos/" + filename;
            }
            tbHinhSanPhamBUS.Uptate(ha);
        }
        catch
        {
        }
        Response.Redirect("~/admin/Danhsachsanpham.aspx");
        Response.Redirect(Request.RawUrl);
    }
}
